﻿using System;
using System.Collections.Generic;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TracNghiemV1.Database;

namespace TracNghiemV1.DAO
{
    class QuestionItemDao
    {
        private static TestDataDataContext m_dataContext = null;

        public static void AddQuestionItem(QUESTION_ITEM newQuestionItem)
        {

            m_dataContext = DataContextDao.GetInstance();

            m_dataContext.QUESTION_ITEMs.InsertOnSubmit(newQuestionItem);
            m_dataContext.SubmitChanges();
        }

        public static void DeleteQuestionItem(int questionId)
        {
            m_dataContext = DataContextDao.GetInstance();

            IList<QUESTION_ITEM> lstQS = FindQuestionItemListWithQuestionId(questionId);

            foreach (QUESTION_ITEM qt in lstQS)
            {
                m_dataContext.QUESTION_ITEMs.DeleteOnSubmit(qt);
            }


            m_dataContext.SubmitChanges();
        }

        public static IList<QUESTION_ITEM> FindQuestionItemListWithQuestionId(int questionId)
        {
            m_dataContext = DataContextDao.GetInstance();

            var questionItems = from p in m_dataContext.QUESTION_ITEMs
                                    where p.QUESTION_ID == questionId
                                    select p;
            return questionItems.ToList();
        }

    }
}
